﻿using M_Purchase.Api.Appliction.Command.AllocateCommand;
using M_Purchase.Repository.PurchaseInterface;
using MediatR;

namespace M_Purchase.Api.Appliction.CommandHandlers.AllocateCommandHandler
{
    /// <summary>
    /// 更新调拨单状态命令处理器
    /// </summary>
    public class UpdateStatusAllocateCommandHandler : IRequestHandler<UpdateStatusAllocateCommand, int>
    {
        /// <summary>
        /// 调拨仓储接口
        /// </summary>
        protected readonly IAllocateRepository allocateRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="allocateRepository">调拨仓储接口</param>
        public UpdateStatusAllocateCommandHandler(IAllocateRepository allocateRepository)
        {
            this.allocateRepository = allocateRepository;
        }

        public async Task<int> Handle(UpdateStatusAllocateCommand request, CancellationToken cancellationToken)
        {
            var res = await allocateRepository.GetModelsAsync(request.AllocateId);
            if (request.AllocateStatus == "审核中")
            {
                res.AllocateStatus = "审核中";
            }
            else if (request.AllocateStatus == "已驳回")
            {
                res.AllocateStatus = "已驳回";
            }
            else if (request.AllocateStatus == "已通过")
            {
                res.AllocateStatus = "已通过";
            }
            return await allocateRepository.UpdateAsync(res);
        }
    }
}
